//
//  MSRoomManagementController.m
//  觅尚
//
//  Created by No.8 on 2019/3/27.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSRoomManagementController.h"
#import "MSRoomManagementCell.h"
#import "MSRoomConsumptionController.h"
#import "MSRoomManageMentModel.h"
#import "MSPrincessessModel.h"
#import "MSPreciousSaleInfoModel.h"



// 注意const的位置
static NSString *const cellId = @"cellId";
static NSString *const headerId = @"headerId";
static NSString *const footerId = @"footerId";
@interface MSRoomManagementController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    CustomNavigationBar *naviagtionBar;
}

/** tableview */
@property (strong,nonatomic)UICollectionView * CollectionView;

/** 头部视图 */
@property(strong,nonatomic)UIView *CollectionHeaderView;


/** 数组*/
@property(nonatomic,strong)NSMutableArray *RoomManagementArray;

/********成员属性********/
/** 房间个数*/
@property(nonatomic,strong)UILabel * totleCountLabel;
@end

@implementation MSRoomManagementController

- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
    self.RoomManagementArray = [NSMutableArray new];
    
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.CollectionView.mj_header = header;
    [self.CollectionView.mj_header beginRefreshing];
    
}
- (void)MJChiBaoZiHeaderRefresh{

    [self ReloadData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self CreatCollectionHeaderView];
    [self CreateNavigationBar];
    
    [self CreatCollectionView];
}
- (void)ReloadData{
    [self.RoomManagementArray removeAllObjects];
    NSDictionary * params = @{@"busid":[WYCAccountTool unarchivebelongBusid],@"uid":[WYCAccountTool unarchiveuid],@"type":@"4"};
//    WYCLog(@"---------%@",params);
    [HTTPRequest POST:MSqueryRoomInformation params:params succeed:^(id responseObject) {
         WYCLog(@"---------%@",responseObject);
        if (CodeStr) {
            [self.CollectionView.mj_header endRefreshing];
            self.totleCountLabel.text = [NSString stringWithFormat:@"房间总量: %@间   开房量:%@间",responseObject[@"data"][@"roomRuningSize"],responseObject[@"data"][@"roomOpenSize"]];
            
            self.RoomManagementArray = [MSRoomManageMentModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"roomRuningList"]];
        }else{
            [self.CollectionView.mj_header endRefreshing];
            [MBManager showBriefAlert:responseObject[@"msg"]];
        }
        [self.CollectionView reloadData];
    } failure:^(NSError *error) {
        [self.CollectionView.mj_header endRefreshing];
        [MBManager showBriefAlert:Failed];
    }];
}
- (void)CreatCollectionHeaderView{
    
    UIView * HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, [self navBarBottom], ScreenWidth, 300 * px)];
    HeaderView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:HeaderView];
//    self.CollectionHeaderView = HeaderView;
    
    /** 房间总量*/
    UILabel * totleCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(40 * px, 40 * px, ScreenWidth / 3 * 2, 80 * px)];
    totleCountLabel.textColor = [UIColor whiteColor];
    totleCountLabel.text = @"房间总量:70间     开房量:50间";
    totleCountLabel.textAlignment = NSTextAlignmentLeft;
    totleCountLabel.font = [UIFont systemFontOfSize:SYRealValue(34 / 2)];
    [HeaderView addSubview:totleCountLabel];
    self.totleCountLabel = totleCountLabel;
    
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(totleCountLabel.frame) + 40 * px, ScreenWidth, 1)];
    lineView.backgroundColor = BackViewColor;
    [HeaderView addSubview:lineView];
    
    
    /** 开房标记*/
    UIImageView * GetroomIMG = [[UIImageView alloc] initWithFrame:CGRectMake(40 * px, CGRectGetMaxY(lineView.frame) + 80 * px, 30 * px, 30 * px)];
    GetroomIMG.backgroundColor = WYCRGBColor(122, 106, 224);
    GetroomIMG.layer.cornerRadius = 10 * px;
    [HeaderView addSubview:GetroomIMG];
    
    /** 开房label*/
    UILabel * Getroomlabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(GetroomIMG.frame) + 5 * px, GetroomIMG.y - 20 * px, 200 * px, 60 * px)];
    Getroomlabel.textColor = WYCRGBColor(126, 138, 155);
    Getroomlabel.text = @"开房";
    Getroomlabel.font = [UIFont systemFontOfSize:SYRealValue(32 / 2)];
    [HeaderView addSubview:Getroomlabel];
    
    
    
    /** 预约标记*/
    UIImageView * AppointmentIMG = [[UIImageView alloc] initWithFrame:CGRectMake((HeaderView.width + 80 * px) / 4, GetroomIMG.y, GetroomIMG.width, GetroomIMG.height)];
    AppointmentIMG.backgroundColor = WYCRGBColor(97, 203, 183);
    AppointmentIMG.layer.cornerRadius = 10 * px;
    [HeaderView addSubview:AppointmentIMG];

    /** 预约label*/
    UILabel * Appointmentlabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(AppointmentIMG.frame) + 5 * px, Getroomlabel.y, 200 * px, 60 * px)];
    Appointmentlabel.textColor = Getroomlabel.textColor;
    Appointmentlabel.text = @"预约";
    Appointmentlabel.font = [UIFont systemFontOfSize:SYRealValue(32 / 2)];
    [HeaderView addSubview:Appointmentlabel];
    
    
    /** 超消费标记*/
    UIImageView * OverConsumptionIMG = [[UIImageView alloc] initWithFrame:CGRectMake((HeaderView.width + 80 * px) / 2, GetroomIMG.y, GetroomIMG.width, GetroomIMG.height)];
    OverConsumptionIMG.backgroundColor = WYCRGBColor(224, 93, 49);
    OverConsumptionIMG.layer.cornerRadius = 10 * px;
    [HeaderView addSubview:OverConsumptionIMG];
    
    /** 超消费label*/
    UILabel * OverConsumptionlabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(OverConsumptionIMG.frame) + 5 * px, Getroomlabel.y, 200 * px, 60 * px)];
    OverConsumptionlabel.textColor = Getroomlabel.textColor;
    OverConsumptionlabel.text = @"超消费";
    OverConsumptionlabel.font = [UIFont systemFontOfSize:SYRealValue(32 / 2)];
    [HeaderView addSubview:OverConsumptionlabel];
    
    
    
    /** 未使用标记*/
    UIImageView * NotusedIMG = [[UIImageView alloc] initWithFrame:CGRectMake((HeaderView.width + 80 * px) / 4 * 3, GetroomIMG.y, GetroomIMG.width, GetroomIMG.height)];
    NotusedIMG.backgroundColor = WYCRGBColor(144, 134, 159);
    NotusedIMG.layer.cornerRadius = 10 * px;
    [HeaderView addSubview:NotusedIMG];
    
    /** 招待label*/
    UILabel * Notusedlabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(NotusedIMG.frame) + 5 * px, Getroomlabel.y, 200 * px, 60 * px)];
    Notusedlabel.textColor = Getroomlabel.textColor;
    Notusedlabel.text = @"未使用";
    Notusedlabel.font = [UIFont systemFontOfSize:SYRealValue(32 / 2)];
    [HeaderView addSubview:Notusedlabel];
    
}

- (void)CreatCollectionView{
    
    //1.初始化layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置collectionView滚动方向
    //
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    //设置headerView的尺寸大小
    layout.headerReferenceSize = CGSizeMake(ScreenWidth, self.CollectionHeaderView.height);
    //该方法也可以设置itemSize
    //    layout.itemSize = CGSizeMake(744 * px, 438 * px);
    //2.初始化collectionView
    self.CollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, [self navBarBottom] + 320 * px, ScreenWidth, ScreenHeight - [self navBarBottom] - 320 * px) collectionViewLayout:layout];
    
    
    self.CollectionView.backgroundColor = [UIColor clearColor];
    //3.注册collectionViewCell //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
    [self.CollectionView registerClass:[MSRoomManagementCell class] forCellWithReuseIdentifier:cellId];
    //注册headerView 此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为reusableView
//    [self.CollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId];
    
    self.CollectionView.showsVerticalScrollIndicator = NO;
    //4.设置代理
    self.CollectionView.delegate = self;
    
    self.CollectionView.dataSource = self;
    
    [self.view addSubview:self.CollectionView];
}

#pragma mark collectionView代理方法
//返回section个
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
    
}
//设置每个分区的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.RoomManagementArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    /* 查找可复用的单元格，自定义需要注册 */
    MSRoomManagementCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.RoomManageMentModel = self.RoomManagementArray[indexPath.row];
    
    return cell;
    
}
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(ScreenWidth / 2 - 10 * px, 700 * px);
    
}
////header的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
//{
//    if (section == 0) {
//        return CGSizeMake(ScreenWidth, self.CollectionHeaderView.height);
//    }
//    else {
//        return CGSizeMake(0, 0);
//    }
//}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
    
}
//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10 * px;
    
}
//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 20 * px;
    
}
////通过设置SupplementaryViewOfKind 来设置头部或者底部的view，其中 ReuseIdentifier 的值必须和 注册是填写的一致，本例都为 “reusableView”
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//        UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerId forIndexPath:indexPath];
//
//        header.backgroundColor = backgroundcolor;
//        [header addSubview:self.CollectionHeaderView];;
//        return header;
//    }
//    return nil;
//
//
//    //
//}

#pragma mark -UICollectionViewDelegate
//UICollectionView被选中的时候调用
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%zd",indexPath.item);
    MSRoomManageMentModel * RoomManageMentModel = self.RoomManagementArray[indexPath.row];
    MSRoomConsumptionController * RommVC = [[MSRoomConsumptionController alloc] init];
    RommVC.roomID = RoomManageMentModel.roomId;
    RommVC.Tagstr = @"2";
    [self.navigationController pushViewController:RommVC animated:NO];
    
}
// 返回这个UICollectionView是否可以被选择
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
    
}
- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"房间管理";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    naviagtionBar.leftBtn.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
////    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
//    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}

//- (void)backClick{
//    
//    [self.navigationController popViewControllerAnimated:NO];
//}
@end
